﻿using System;
using System.Windows.Controls;
using System.Windows;
using System.ComponentModel;
using System.Windows.Input;


namespace WickedFlame.GenDock
{
    internal class ButtonBase : ContentControl, INotifyPropertyChanged
    {
        public ButtonBase()
            : base()
        {
        }

        public bool IsSelected
        {
            get
            {
                return (bool)GetValue(IsSelectedProperty);
            }
            set
            {
                SetValue(IsSelectedProperty, value);
                NotifyPropertyChanged("IsSelected");
            }
        }

        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(ButtonBase));

        public object Icon
        {
            get
            {
                return (object)GetValue(IconProperty);
            }
            set
            {
                SetValue(IconProperty, value);
            }
        }

        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register("Icon", typeof(object), typeof(ButtonBase));

        public string Title
        {
            get
            {
                return (string)GetValue(TitleProperty);
            }
            set
            {
                SetValue(TitleProperty, value);
            }
        }

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(ButtonBase));
        
        public event RoutedEventHandler Click;
        public bool IsPressed
        {
            get
            {
                return (bool)GetValue(IsPressedProperty);
            }
            set
            {
                SetValue(IsPressedProperty, value);
                NotifyPropertyChanged("IsPressed");
            }
        }

        public static readonly DependencyProperty IsPressedProperty =
            DependencyProperty.Register("IsPressed", typeof(bool), typeof(ButtonBase),new PropertyMetadata(false));
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            IsPressed = true;
            base.OnMouseLeftButtonDown(e);
        }
        protected override void OnMouseLeave(MouseEventArgs e)
        {
            if (IsPressed)
                IsPressed = false;
            base.OnMouseLeave(e);
        }
        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            IsPressed = false;
            if (IsSelected)
                IsSelected = false;
            else
                IsSelected = true;

            if(Click!=null)
                Click(this, new RoutedEventArgs());

            base.OnMouseLeftButtonUp(e);
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        
    }
}
